package com.buer.storage.controller;

import com.buer.storage.commom.JsonResult;
import com.buer.storage.dto.*;
import com.buer.storage.entity.*;
import com.buer.storage.service.DorderService;
import com.buer.storage.utils.CheckEmptyUtil;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/out")
public class DorderController {
    @Resource
    private DorderService dorderService;

    @GetMapping("/findStock.do")
    public List<Stock> findStock() {
        return dorderService.findStock();
    }

    //根据出库ID查询
    @PostMapping("/selectByAll.do")
    public JsonResult selectByAll(@RequestBody Map<String, Object> params) {
        PageInfo<Dorder> entityList = dorderService.selectByAll(params);
        Map result = new HashMap<>();
        result.put("total", entityList.getTotal());
        result.put("list", entityList.getList());
        return new JsonResult(1, result);
    }


    @PostMapping("/insert.do")
    public R insert(@RequestBody OutStorageDto outStorageDto) {
        if (dorderService.insert(outStorageDto) > 0) {

            return R.setR(1, "出库单保存成功", null);
        }

        return R.setR(0, "出库单保存失败", null);
    }

    @GetMapping("/findOrderAll.do")
    public List<DorderDto> findOrderAll() {
        List<DorderDto> outAll = dorderService.findOrderAll();
        return outAll;
    }

    //根据订单ID查询入库信息
    @GetMapping("/findOutAll.do")
    public List<OutStorageAllDto> findOutAll(Integer outId) {
        List<OutStorageAllDto> outAll = dorderService.findOutAll(outId);
        return outAll;
    }

    //根据入库订单ID删除出库信息
    @GetMapping("/deleteOutStorageById.do")
    public R deleteOutStorageById(int outId) {
        if (dorderService.deleteOutStorageById(outId) > 0) {
            return R.setR(1, "删除出库单成功", null);
        }
        return R.setR(0, "删除出库单失败", null);
    }

    //根据商品ID删除商品信息
    @GetMapping("/deleteDorderdetails.do")
    public R deleteDorderdetails(int deliveryId) {
        if (dorderService.deleteDorderdetails(deliveryId) > 0) {
            return R.setR(1, "删除出库订单成功", null);
        }
        return R.setR(0, "删除出库订单失败", null);
    }

    //更新ID删除商品信息
    @PostMapping("/update.do")
    public R update(@RequestBody OutStorageDto outStorageDto) {
        if (dorderService.updateDorderdetails(outStorageDto) > 0) {
            return R.setR(1, "更新出库单成功", null);
        }
        return R.setR(0, "更新出库单失败", null);
    }

    //提交商品详情进行累加
    @GetMapping("/submitAddDorderdetails.do")
    public R submitAddBuerStock(int outId) {
        if (dorderService.submitAddBuerStock(outId) > 0) {
            return R.setR(1, "出库成功", null);
        }
        return R.setR(0, "出库失败", null);
    }

    @PostMapping("/getDord")
    public JsonResult getDord(@RequestBody Map<String, Object> params) {
        PageInfo<DorderDto> entityList = dorderService.getDord(params);
        Map result = new HashMap<>();
        result.put("total", entityList.getTotal());
        result.put("list", entityList.getList());
        return new JsonResult(1, result);
    }

    @PostMapping("/getDorderdetails")
    public JsonResult getDorderdetails(@RequestBody Map<String, Object> params) {
        PageInfo<DorderdetailsDto> entityList = dorderService.getDorderdetails(params);
        Map result = new HashMap<>();
        result.put("total", entityList.getTotal());
        result.put("list", entityList.getList());
        return new JsonResult(1, result);
    }

    @PostMapping("/selectByDate")
    public JsonResult selectByDate(@RequestBody Map<String, Object> params) {

        PageInfo<StockDto> entityList = dorderService.selectByDate(params);
        Map result = new HashMap<>();
        result.put("total", entityList.getTotal());
        result.put("list", entityList.getList());
        return new JsonResult(1, result);
//        List<StockDto> outAll = dorderService.selectByDate();
//        return outAll;
    }


    @PostMapping("/updateDorderdetails.do")
    public R updateDorderdetails(@RequestBody OutBatchOrderDto outBatchOrderDto) {
        dorderService.updateOrder(outBatchOrderDto);
        List<BatchOrderDto> list = outBatchOrderDto.getList();
        if (CheckEmptyUtil.isNotEmpty(list)) {
            for (BatchOrderDto outBatchDto : list) {
                    if (outBatchDto.getType().equals("add")) {
                        dorderService.insertBatch(outBatchDto);
                    } else if (outBatchDto.getType().equals("edit")) {
                        dorderService.updateBatch(outBatchDto);

                    } else if (outBatchDto.getType().equals("delete")) {
                        dorderService.deleteBatch(outBatchDto);
                    }
                }
            }
        return R.setR(1, "修改成功", null);
    }
}
